package com.ruitu.mall_98_1.product.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.ToolBarFragment;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.utils.CalcUtils;
import com.beanu.arad.utils.ScreenUtils;
import com.beanu.arad.utils.SizeUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.utils.UIUtils;
import com.beanu.arad.widget.MyScrollView;
import com.bumptech.glide.Glide;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.product.activity.PhotoCheckActivity;
import com.ruitu.mall_98_1.product.activity.ProductDetailActivity;
import com.ruitu.mall_98_1.product.activity.ShopDetailActivity;
import com.ruitu.mall_98_1.product.activity.VideoCheckActivity;
import com.ruitu.mall_98_1.product.adapter.CouponListAdapter;
import com.ruitu.mall_98_1.product.adapter.GroupUserAdapter;
import com.ruitu.mall_98_1.product.adapter.ProductListAdapter;
import com.ruitu.mall_98_1.product.adapter.SectionListAdapter;
import com.ruitu.mall_98_1.product.support.AlertUtil;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.DataAdapter;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.product.ProDetail;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.bean.product.Section;
import com.ruitu.router_module.utils.GlideUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class ProductFragment extends ToolBarFragment implements ProductDetailActivity.ProDetail1Listener {
    private ViewPager vp_img;
    private RelativeLayout rl_img;//vp的外层布局
    private TextView tv_indicator;//指示器
    //    private SlideDetailsLayout sv_switch;//上拉加载详情控件
    private MyScrollView scrollView;//上半部分
    private TextView tv_section;//区间价格
    private WebView webview;//用于显示商品详情
    private TextView tv_taocan_name, tv_brief, tv_price, tv_old_price, tv_group;//套餐名称,套餐介绍,现价,原价,立即拼团
    private ImageView iv_icon;//店铺的图片
    private TextView tv_name, tv_saling_num, tv_evaluate_num, tv_pre_sale, tv_group_0;//店铺名,在售商品数,评价数,预售,正在拼团
    private LinearLayout ll_coupon, ll_section, ll_evaluate, ll_choose, ll_tags;//领店铺优惠券,区间价,商品评价,选择规格数量,所有标签的容器
    private LinearLayout ll_video, ll_pre_sale, ll_group, ll_shop;//视频,预售,团购模块,店铺布局
    private RecyclerView rcv_pro, rcv_group, recyclerView;//横向商品列表,拼团用户列表.全局商品列表
    TextView tag_zidingyi, tag_pintuan, tag_qujianjia, tag_yuding, tag_yushou;

    private ProductListAdapter adapter;//横向的推荐商品列表adapter
    private DataAdapter globalAdapter;

    Product p;//请求完成,传过来的商品对象
    ProDetail proDetail;//请求完成,传过来的商品详情对象
    private List<ProDetail.Img> imgList = new ArrayList<>();//套餐的轮播图
    private VpImgAdapter imgAdapter = new VpImgAdapter();

    private CouponListAdapter couponAdapter;//优惠券列表adapter
    private SectionListAdapter sectionAdapter;//区间价adapter
    private AlertDialog alertCoupon;//领优惠券的弹窗
    private AlertDialog alertSection;//区间价的弹窗

    public static ProductFragment newInstance() {
        Bundle args = new Bundle();
        ProductFragment fragment = new ProductFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private int SCREEN_WIDTH = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);
        SCREEN_WIDTH = ScreenUtils.getScreenWidth();

        ((ProductDetailActivity) (getActivity())).setProDetail1Listener(this);
        adapter = new ProductListAdapter(getActivity());
        adapter.type = 1;
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
//                p = (Product) bundle.getSerializable("product");
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", proDetail.getGoodsList().get(position));
                startActivity(ProductDetailActivity.class, bundle);
            }
        });

        couponAdapter = new CouponListAdapter(getActivity());
        sectionAdapter = new SectionListAdapter(getActivity());
        alertCoupon = new AlertDialog.Builder(getActivity(), R.style.Dialog_FS).create();
        alertSection = new AlertDialog.Builder(getActivity(), R.style.Dialog_FS).create();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private int detailY = 0;//详情点的y坐标

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetEvent(EventModel e) {
        if (e.getEventCode() == 18102951) {
            int[] position = new int[2];
            webview.getLocationInWindow(position);
            detailY = position[1];
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_product, container, false);
        vp_img = v.findViewById(R.id.vp_img);
        rl_img = v.findViewById(R.id.rl_img);
        tv_indicator = v.findViewById(R.id.tv_indicator);
//        sv_switch = v.findViewById(R.id.sv_switch);
        scrollView = v.findViewById(R.id.scrollView);
        webview = v.findViewById(R.id.webview);
        tv_taocan_name = v.findViewById(R.id.tv_taocan_name);
        tv_brief = v.findViewById(R.id.tv_brief);
        tv_price = v.findViewById(R.id.tv_price);
        tv_old_price = v.findViewById(R.id.tv_old_price);
        UIUtils.setDelLine(tv_old_price);
        iv_icon = v.findViewById(R.id.iv_icon);
        tv_name = v.findViewById(R.id.tv_name);
        tv_saling_num = v.findViewById(R.id.tv_saling_num);
        tv_evaluate_num = v.findViewById(R.id.tv_evaluate_num);
        tv_pre_sale = v.findViewById(R.id.tv_pre_sale);
        tv_group_0 = v.findViewById(R.id.tv_group_0);
        ll_coupon = v.findViewById(R.id.ll_coupon);
        ll_section = v.findViewById(R.id.ll_section);
        ll_pre_sale = v.findViewById(R.id.ll_pre_sale);
        ll_group = v.findViewById(R.id.ll_group);
        ll_shop = v.findViewById(R.id.ll_shop);
        rcv_group = v.findViewById(R.id.rcv_group);
        ll_evaluate = v.findViewById(R.id.ll_evaluate);
        rcv_pro = v.findViewById(R.id.rcv_pro);
        ll_choose = v.findViewById(R.id.ll_choose);
        ll_video = v.findViewById(R.id.ll_video);
        tag_zidingyi = v.findViewById(R.id.tag_zidingyi);
        tag_pintuan = v.findViewById(R.id.tag_pintuan);
        tag_qujianjia = v.findViewById(R.id.tag_qujianjia);
        tag_yuding = v.findViewById(R.id.tag_yuding);
        tag_yushou = v.findViewById(R.id.tag_yushou);
        ll_tags = v.findViewById(R.id.ll_tags);
        tv_section = v.findViewById(R.id.tv_section);
        tv_group = v.findViewById(R.id.tv_group);
        recyclerView = v.findViewById(R.id.recyclerView);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rl_img.getLayoutParams();
        params.width = SCREEN_WIDTH;//设置宽高一致的,都为屏幕宽度(方形大图)
        params.height = SCREEN_WIDTH;//设置宽高一致的,都为屏幕宽度(方形大图)
        rl_img.setLayoutParams(params);

        tv_price.setOnClickListener(this);
        ll_coupon.setOnClickListener(this);
        ll_section.setOnClickListener(this);
        ll_evaluate.setOnClickListener(this);
        ll_choose.setOnClickListener(this);
        ll_video.setOnClickListener(this);
        tv_group.setOnClickListener(this);
        ll_group.setOnClickListener(this);
        ll_shop.setOnClickListener(this);
        vp_img.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                setIndicator(position, false);
            }
        });

        scrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                if (detailY > 0) {
                    if (scrollY >= detailY - SizeUtils.dp2px(75)) {
                        ((ProductDetailActivity) getActivity()).setAsProIntro(2);//详情指示器
                    } else if (scrollY < SCREEN_WIDTH) {
                        ((ProductDetailActivity) getActivity()).setAsProIntro(0);//商品指示器
                    }
                }
            }
        });

        return v;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == ll_evaluate) {//商品评价
            ((ProductDetailActivity) getActivity()).setAsProIntro(1);//切换到评价页面
        }
        if (v == ll_choose) {
            ((ProductDetailActivity) getActivity()).showDialog();//弹窗选择规格数量
        }
        if (v == ll_coupon) {//领券
            showDialogCoupon();
        }
        if (v == ll_section || v == tv_price) {//区间价
            showDialogSection();
        }
        if (v == ll_video) {//视频
            Bundle bundle = new Bundle();
            bundle.putString("video_url", Config.BASE_URL + p.getVideoUrl());
            startActivity(VideoCheckActivity.class, bundle);
            getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
        if (v == tv_group || v == ll_group) {//立即拼团
            ((ProductDetailActivity) getActivity()).showDialog();
        }
        if (v == ll_shop) {//进店看看
            if (null == proDetail || null == proDetail.getCompany()) {
                ToastUtils.showShort("数据加载中,请稍候再试");
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("companyId", proDetail.getCompany().getId());
                startActivity(ShopDetailActivity.class, bundle);
            }
        }
    }

    private View alert_view_coupon;//对话框的布局,优惠券
    private View alert_view_section;//对话框的布局,区间价

    public void showDialogCoupon() {//优惠券弹窗
        if (CommonUtils.isLoginToActivity(getActivity())) {
            if (null == proDetail) {
                ToastUtils.showShort("数据加载中,请稍候");
                return;
            }
            if (isDataEmpty(proDetail.getCoupons())) {
                ToastUtils.showShort("暂无优惠券");
                return;
            }
            alertCoupon.show();
            alert_view_coupon = View.inflate(getActivity(), R.layout.pro_alert_coupon, null);
            alert_view_coupon.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertCoupon.dismiss();//点击优惠券的关闭按钮
                }
            });
            RecyclerView rcv_coupon = alert_view_coupon.findViewById(R.id.rcv_coupon);
            rcv_coupon.setLayoutManager(new LinearLayoutManager(getActivity()));
            rcv_coupon.setAdapter(couponAdapter);
            couponAdapter.setDataList(proDetail.getCoupons());
            AlertUtil.setAlertBottomInStyle(alertCoupon.getWindow(), alert_view_coupon, getActivity());
        } else {
            ToastUtils.showShort("请先登录");
        }
    }

    public void showDialogSection() {//区间价弹窗
        if (null == proDetail) {
            ToastUtils.showShort("数据加载中,请稍候");
            return;
        }
        if (isDataEmpty(proDetail.getGoodsSections())) {
//            ToastUtils.showShort("该商品不设区间价");
            return;
        }
        alertSection.show();
        alert_view_section = View.inflate(getActivity(), R.layout.pro_alert_section, null);
        alert_view_section.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertSection.dismiss();//点击区间价的关闭按钮
            }
        });
        RecyclerView rcv_section = alert_view_section.findViewById(R.id.rcv_section);
        rcv_section.setLayoutManager(new LinearLayoutManager(getActivity()));
        rcv_section.setAdapter(sectionAdapter);
        sectionAdapter.setDataList(proDetail.getGoodsSections());
        AlertUtil.setAlertBottomInStyle(alertSection.getWindow(), alert_view_section, getActivity());
    }

    private void initWebview(String url) {
        webview.getSettings().setJavaScriptEnabled(true);//开启js的支持
        //自适应屏幕
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示
//        webview.setWebViewClient(new WebViewClient() { //调用自身，不调用系统浏览器
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (TextUtils.isEmpty(url)) {
//                } else {
//                    if (url.startsWith("tmall://")) {
//
//                    } else {
//                        view.loadUrl(url);
//                    }
//                }
//                return true;
//            }
//        });
        webview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activity和Webview根据加载程度决定进度条的进度大小
                // 当加载到100%的时候 进度条自动消失
                if (progress == 100) {
                }
            }
        });
        webview.loadUrl(url);
    }

    //设置数字指示器
    private void setIndicator(int currPos, boolean is0) {
        if (is0) {
            tv_indicator.setText(0 + "/" + 0);
        } else {
            try {
                tv_indicator.setText(currPos + 1 + "/" + imgList.size());
            } catch (Exception e) {
                tv_indicator.setText(0 + "/" + 0);
            }
        }
    }

    //设置商品信息
    private void setProInfo() {
        tv_price.setText("￥" + p.getGoodMarket());
        tv_old_price.setText("￥" + p.getOriginalPrice());
        tv_taocan_name.setText(p.getGoodName());
        if (TextUtils.equals("1", proDetail.getCompany().getIntegralStatus())) {// 开通送积分
            String txt = "由" + proDetail.getCompany().getName() + "发货并负责售后服务。" + "购买商品送积分！";
            SpannableString ss = new SpannableString(txt);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            int start = txt.length() - 8;
            ss.setSpan(redSpan, start, start + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            tv_brief.setText(ss);
        } else {
            tv_brief.setText("由" + proDetail.getCompany().getName() + "发货并负责售后服务。");
        }
        tv_evaluate_num.setText("商品评价(" + proDetail.getGoodsCommentCount() + ")");
        tv_name.setText(proDetail.getCompany().getName());
        tv_saling_num.setText("在售商品" + proDetail.getCompany().getTotalGoods() + "件");
        setTags();//设置各种标签
        GlideUtil.loadImg(Config.BASE_URL + proDetail.getCompany().getCompanyUrl(), iv_icon);
        initWebview(Config.BASE_URL + p.getGoodContent());//商品详情url页面
        //是否显示区间价
        if (null != proDetail && null != p && TextUtils.equals("1", p.getIntervalStatus())
                && !isDataEmpty(proDetail.getGoodsSections())) {
            ll_section.setVisibility(View.VISIBLE);
            Section sec = proDetail.getGoodsSections().get(0);

//            tv_section.setText("区间价：购买数量在" + sec.getBeginNum() + "--" + sec.getEndNum()
//                    + "之间优惠" + sec.getDiscountProportion() + "%。点击查看更多区间优惠");
            // 优惠改为折扣
            Double d1 = sec.getDiscountProportion();
            if (null != d1) {
                String discount = CalcUtils.sub(100D, d1) + "";
                tv_section.setText("区间价：购买数量在" + sec.getBeginNum() + "--" + sec.getEndNum()
                        + "之间折扣" + discount + "%。点击查看更多区间优惠");
            } else {
                tv_section.setText("区间价：购买数量在" + sec.getBeginNum() + "--" + sec.getEndNum()
                        + "之间折扣" + 100 + "%。点击查看更多区间优惠");
            }
        } else {
            ll_section.setVisibility(View.GONE);
        }
        //是否显示领券
        if (null != proDetail && !isDataEmpty(proDetail.getCoupons())) {
            ll_coupon.setVisibility(View.VISIBLE);
        } else {
            ll_coupon.setVisibility(View.GONE);
        }
        //是否显示预售
        if (null != proDetail && null != proDetail.getGoodsPreSale() && !TextUtils.isEmpty(proDetail.getGoodsPreSale().getId())) {
            ll_pre_sale.setVisibility(View.VISIBLE);
            ProDetail.PreSale pre = proDetail.getGoodsPreSale();
            tv_pre_sale.setText("预付" + pre.getPreSaleRatio() + "%下单后" + pre.getPreSaleDelivey() + "天发货");
        } else {
            ll_pre_sale.setVisibility(View.GONE);
        }
        //是否显示团购信息
        if (null != proDetail && !isDataEmpty(proDetail.getCollageUsers())) {
            ll_group.setVisibility(View.VISIBLE);
            tv_group_0.setText("正在拼团" + p.getCountCollageNow() + "/" + p.getCountCollage());
            GroupUserAdapter guAdapter = new GroupUserAdapter(getActivity());
            LinearLayoutManager m = new LinearLayoutManager(getActivity());
            m.setOrientation(LinearLayoutManager.HORIZONTAL);
            rcv_group.setLayoutManager(m);
            rcv_group.setAdapter(guAdapter);
            guAdapter.setDataList(proDetail.getCollageUsers());
        } else if (p.getCountCollage() > 0) {//当前拼团人数...说明是拼团商品,暂时还没有人拼
            ll_group.setVisibility(View.VISIBLE);
            tv_group_0.setText("正在拼团" + 0 + "/" + p.getCountCollage());
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rcv_group.getLayoutParams();
            params.height = 1;
            params.topMargin = 1;
            rcv_group.setLayoutParams(params);
        } else {
            ll_group.setVisibility(View.GONE);
        }
        //商品大图
        imgList = proDetail.getImageList();
        if (!isDataEmpty(imgList)) {
            setIndicator(0, false);
        } else {
            setIndicator(0, true);
        }
        vp_img.setAdapter(imgAdapter);
        //是否有视频
        if (TextUtils.isEmpty(p.getVideoUrl())) {
            ll_video.setVisibility(View.GONE);
        } else {
            ll_video.setVisibility(View.VISIBLE);
        }
        //详情页面的横向推荐商品列表
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcv_pro.setLayoutManager(manager);
        rcv_pro.setAdapter(adapter);
        adapter.setDataList(proDetail.getGoodsList());

        globalAdapter = new DataAdapter(getActivity());
        GridLayoutManager manager1 = new GridLayoutManager(getActivity(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(manager1);
        recyclerView.setAdapter(globalAdapter);
        globalAdapter.setDataList(AppHolder.instance().getGlobalProList());
        globalAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                Product product = new Product();
                product.setGoodId(AppHolder.instance().getGlobalProList().get(position).getGoodsId());
                bundle.putSerializable("product", product);
                startActivity(ProductDetailActivity.class, bundle);
            }
        });
    }

    private boolean isDetailOk = false;//webView是否加载完成

    @Override
    public void onDetailClick() {//详情
        try {
            if (detailY >= SizeUtils.dp2px(48)) {
                if (isDetailOk) {
                    scrollView.scrollTo(0, detailY - SizeUtils.dp2px(75));
                } else {
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.scrollTo(0, detailY - SizeUtils.dp2px(75));
                        }
                    }, 100);
                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onProClick() {//商品介绍
        try {
            scrollView.scrollTo(0, 0);
        } catch (Exception e) {
        }
    }

    @Override
    public void onGetProInfo(ProDetail proDetail) {
        this.p = proDetail.getGoods();
        this.proDetail = proDetail;
        setProInfo();
    }

    private void setTags() {//设置各种标签
        try {
            if (!TextUtils.isEmpty(p.getGoodTagName())) {//是否有自定义标签
                tag_zidingyi.setVisibility(View.VISIBLE);
                tag_zidingyi.setText(p.getGoodTagName());//设置自定义标签名称
            } else {
                tag_zidingyi.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }
        try {
            if (TextUtils.equals("1", p.getShoppingGroupStatus())) {//是否团购 0 否 1 是
                tag_pintuan.setVisibility(View.VISIBLE);
            } else {
                tag_pintuan.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }
        try {
            if (TextUtils.equals("1", p.getIntervalStatus())) {//是否区间价格 0 否 1 是
                tag_qujianjia.setVisibility(View.VISIBLE);
            } else {
                tag_qujianjia.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }
        try {
            if (TextUtils.equals("1", p.getCustomizedStatus())) {//是否预订 0 否 1 是
                tag_yuding.setVisibility(View.VISIBLE);
            } else {
                tag_yuding.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }
        try {
            if (TextUtils.equals("1", p.getGoodSalesModel())) {//销售模式（1：预售、2：现货)
                tag_yushou.setVisibility(View.VISIBLE);
            } else {
                tag_yushou.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_tags.getLayoutParams();
        if (tag_zidingyi.getVisibility() == View.GONE && tag_pintuan.getVisibility() ==
                View.GONE && tag_qujianjia.getVisibility() == View.GONE && tag_yuding
                .getVisibility() == View.GONE && tag_yushou.getVisibility() == View.GONE) {
            //没有显示标签
            params.topMargin = 1;
        } else {
            params.topMargin = SizeUtils.dp2px(8);
        }
        ll_tags.setLayoutParams(params);
    }

    private class VpImgAdapter extends PagerAdapter {//商品轮播adapter

        @Override
        public int getCount() {
            return null == imgList ? 0 : imgList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView iv = new ImageView(getActivity());
            final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ProDetail.Img img = imgList.get(position);
            Glide.with(Arad.app).load(Config.BASE_URL + img.getUrl()).apply(Arad.opts).into(iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), PhotoCheckActivity.class);
                    intent.putExtra("position", position);
                    ArrayList<String> imgPaths = new ArrayList<>();
                    for (ProDetail.Img i : imgList) {//封装图片路径,传到图片查看页面
                        imgPaths.add(Config.BASE_URL + i.getUrl());
                    }
                    intent.putExtra("list", imgPaths);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                }
            });

            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
